﻿Imports System.Xml

Public Class Language
    Private mPhrase As New Hashtable
    Private mFont As Font

    Public Sub Load(ByVal filename As String)
        Dim xml As New XmlDocument()
        xml.Load(filename)

        Dim parent As XmlNode = xml.ChildNodes(1)
        For Each node As XmlNode In parent
            If node.Name = "phrases" Then
                LoadPhrase(node)
            ElseIf node.Name = "setting" Then
                LoadSetting(node)
            End If
        Next
    End Sub

    Private Sub LoadFont(ByVal xml As XmlNode)
        Dim fontName As String = "Segoe UI"
        Dim fontSize As Integer = 9

        For Each node As XmlNode In xml
            If node.Name = "name" Then
                fontName = node.InnerText()
            ElseIf node.Name = "size" Then
                fontSize = CInt(node.InnerText())
            End If
        Next

        mFont = New Font(fontName, fontSize, FontStyle.Regular, GraphicsUnit.Point, Nothing)
    End Sub

    Public Function GetFont()
        Return mFont
    End Function

    Private Sub LoadSetting(ByVal xml As XmlNode)
        For Each node As XmlNode In xml.ChildNodes
            If node.Name = "font" Then
                LoadFont(node)
            End If
        Next
    End Sub

    Private Sub LoadPhrase(ByVal xml As XmlNode)
        For Each node As XmlNode In xml.ChildNodes
            mPhrase(node.Attributes("value").Value.ToString()) = node.InnerText.ToString()
        Next
    End Sub

    Public Function GetPhrase(ByVal phrase As String) As String
        If mPhrase.Contains(phrase) Then
            Return CType(mPhrase(phrase), String)
        Else
            Return ""
        End If
    End Function

    Public Sub SetPhrase(ByVal phrase As String, ByVal value As String)
        mPhrase(phrase) = value
    End Sub
End Class
